﻿using System;
using System.Windows.Forms;
using System.Data.OracleClient;
using TimeTableEasy.src.Database;
using TimeTableEasy.src.Core;

namespace TimeTableEasy
{
    public partial class Campus_Edit : Form
    {
        private OpCodes.TimeTableEasyAction _Action;
        private UInt16 _IdCampus = 0;

        public Campus_Edit(OpCodes.TimeTableEasyAction action, UInt16 id_campus)
        {
            InitializeComponent();

            this._IdCampus = id_campus;

            if (id_campus == 0)
                this._Action = OpCodes.TimeTableEasyAction.ACTION_ADD;
            else
                this._Action = action;

            switch (_Action)
            {
                case OpCodes.TimeTableEasyAction.ACTION_MOD:
                    this.Text = "Modifier un campus";
                    editLbl.Text = "Modifier un campus";
                    OK.Text = "Modifier";

                    OracleDataReader array = OracleConnector.Query("SELECT CAM_NOM, CAM_VILLE FROM CAMPUS WHERE CAM_NUM = " + _IdCampus);

                    if (array == null) return;

                    array.Read();

                    nameTxtBx.Text = array.GetString(0);
                    txtVille.Text = array.GetString(1);

                    array.Dispose();

                    break;
                case OpCodes.TimeTableEasyAction.ACTION_ADD:
                    this.Text = "Ajouter un campus";
                    editLbl.Text = "Ajouter un campus";
                    OK.Text = "Ajouter";
                    break;
            }

        }

        private void OK_Click(object sender, EventArgs e)
        {
            if ((nameTxtBx.Text.Length != 0) || (txtVille.Text.Length != 0))
            {
                if (_Action == OpCodes.TimeTableEasyAction.ACTION_ADD)
                {
                        OracleDataReader array = OracleConnector.Query("INSERT INTO CAMPUS VALUES (Seq_Campus.nextval, '" + nameTxtBx.Text + "', '" + txtVille.Text + "')");

                        if (array == null)
                            Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ADMIN_CAMPUS_ADD_ERROR);
                        else
                            Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ADMIN_CAMPUS_ADD);
                }
                else if (_Action == OpCodes.TimeTableEasyAction.ACTION_MOD)
                {
                        OracleDataReader array = OracleConnector.Query("UPDATE CAMPUS SET CAM_NOM = '" + nameTxtBx.Text + "', CAM_VILLE = '" + txtVille.Text + "' WHERE CAM_NUM = " + this._IdCampus);

                        if (array == null)
                            Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ADMIN_CAMPUS_MOD_ERROR);
                        else
                            Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ADMIN_CAMPUS_MOD);
                }
                else
                    Label_Error.Text = Lang.GetMessage(OpCodes.TimeTableEasyStrings.LANG_ALL_FIELD_NULL);
            }
        }
    }
}
